package com.woniuxy.charin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.charin.enumeration.EventDisposeStatus;
import com.woniuxy.charin.enumeration.EventStatus;
import com.woniuxy.charin.enumeration.EventType;
import com.woniuxy.charin.model.entity.Event;
import com.woniuxy.charin.model.entity.EventDispose;
import com.woniuxy.charin.model.entity.User;
import com.woniuxy.charin.model.request.AssignProcessorsRequest;
import com.woniuxy.charin.model.request.DisposeEventRequest;
import com.woniuxy.charin.model.vo.EventDetailVo;
import com.woniuxy.charin.service.EventDisposeService;
import com.woniuxy.charin.util.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Api(tags = "隐患故障委派处理模块")
@RestController
@RequestMapping("/eventDispose")
public class EventDisposeController {
    @Autowired
    EventDisposeService eventDisposeService;
    @Autowired
    JwtUtil jwtUtil;

    //通用相关操作====================================================
    @ApiOperation("重派处理人")
    @PostMapping("/resendProcessors")
    public ResponseEntity<?> ResendProcessors(@RequestBody AssignProcessorsRequest request,@RequestHeader("Authorization") String token) {
        eventDisposeService.ResendProcessors(request,token);
        return ResponseEntity.ok("重派成功");
    }

    @ApiOperation("指派处理人")
    @PostMapping("/assignProcessors")
    public ResponseEntity<?> assignProcessors(@RequestBody AssignProcessorsRequest request) {
        eventDisposeService.assignProcessors(request);
        return ResponseEntity.ok("指派成功");
    }

    @ApiOperation("处理人处理事件")
    @PostMapping("/disposeEvent")
    public ResponseEntity<?> disposeEvent(@RequestBody DisposeEventRequest request) {
        eventDisposeService.disposeEvent(request);
        return ResponseEntity.ok("处理人处理事件成功");
    }

    @ApiOperation("根据处理id获取事件详情")
    @GetMapping("/getDetailByDisposeId/{disposeId}")
    public ResponseEntity<?> getDetailByDisposeId(@PathVariable Integer disposeId) {
        EventDetailVo eventDetailVo = eventDisposeService.getDetailByDisposeId(disposeId);
        //返回查找结果
        return ResponseEntity.ok(eventDetailVo);
    }

    @ApiOperation("编辑处理详情")
    @PostMapping("/editDisposeDetail")
    public ResponseEntity<?> editDisposeDetail(@RequestBody DisposeEventRequest request) {
        eventDisposeService.editDisposeDetail(request);
        return ResponseEntity.ok("编辑处理详情成功");
    }

    @ApiOperation("获取所有有效处理人")
    @GetMapping("/getAllProcessors")
    public ResponseEntity<?> getAllProcessors() {
        List<User> userList=eventDisposeService.getAllProcessors();
        return ResponseEntity.ok(userList);
    }

    @ApiOperation("获取处理人信息")
    @GetMapping("/getProcessorsInfo")
    public ResponseEntity<?> getProcessorsInfo(Integer processorsId) {
        User user=eventDisposeService.getProcessorsInfo(processorsId);
        return ResponseEntity.ok(user);
    }
    //通用相关操作====================================================
    //隐患相关操作====================================================
    @ApiOperation("admin分页查找隐患处理记录")
    @PostMapping("/pageRiskDisposeByUserId")
    public ResponseEntity<?> pageRiskDisposeByUserId(@RequestBody Map<String, Object> params) {
        //获取页码和页面大小
        Integer pageNum = (Integer) params.get("pageNum");
        Integer pageSize = (Integer) params.get("pageSize");
        //获取状态
        Integer status = (Integer) params.get("status");
        //获取userId
        Integer userId = (Integer) params.get("userId");
        //获取正常且正确的startDate、endDate
        String startDateStr = (String) params.get("startDate");
        String endDateStr = (String) params.get("endDate");
        Date startDate = startDateStr != null ? strToData(startDateStr) : null;
        Date endDate = endDateStr != null ? strToData(endDateStr) : null;
        //调用服务层方法分页查找
        Page<EventDetailVo> page = eventDisposeService.pageRiskDisposeByUserId(userId, pageNum, pageSize, status, startDate, endDate);
        //返回查找结果
        return ResponseEntity.ok(page);
    }

    @ApiOperation("工程师分页查找隐患处理记录")
    @PostMapping("/pageRiskDispose")
    public ResponseEntity<?> pageRiskDispose(@RequestBody Map<String, Object> params,@RequestHeader("Authorization") String token) {
        //获取页码和页面大小
        Integer pageNum = (Integer) params.get("pageNum");
        Integer pageSize = (Integer) params.get("pageSize");
        //获取状态
        Integer status = (Integer) params.get("status");
        //后续添加一个userId（从token中获取）
        Integer userId = Integer.valueOf(jwtUtil.getValue(token,"userId"));
        //获取正常且正确的startDate、endDate
        String startDateStr = (String) params.get("startDate");
        String endDateStr = (String) params.get("endDate");
        Date startDate = startDateStr != null ? strToData(startDateStr) : null;
        Date endDate = endDateStr != null ? strToData(endDateStr) : null;
        //调用服务层方法分页查找
        Page<EventDetailVo> page = eventDisposeService.pageRiskDispose(userId, pageNum, pageSize, status, startDate, endDate);
        //返回查找结果
        return ResponseEntity.ok(page);
    }

    @ApiOperation("统计工程师待处理隐患数")
    @GetMapping("/statisticsCurrentUserRisk")
    public ResponseEntity<?> statisticsRisk(@RequestHeader("Authorization") String token) {
        //后续添加一个userId（从token中获取）
        Integer userId = Integer.valueOf(jwtUtil.getValue(token,"userId"));

        List<Integer> integers = eventDisposeService.statisticsCurrentUserEvent(userId, EventType.RISK.getKey());
        return ResponseEntity.ok(integers);
    }
    //隐患相关操作====================================================
    //故障相关操作====================================================
    @ApiOperation("admin分页查找故障处理记录")
    @PostMapping("/pageFaultDisposeByUserId")
    public ResponseEntity<?> pageFaultDisposeByUserId(@RequestBody Map<String, Object> params) {
        //获取页码和页面大小
        Integer pageNum = (Integer) params.get("pageNum");
        Integer pageSize = (Integer) params.get("pageSize");
        //获取状态
        Integer status = (Integer) params.get("status");
        //获取userId
        Integer userId = (Integer) params.get("userId");
        //获取正常且正确的startDate、endDate
        String startDateStr = (String) params.get("startDate");
        String endDateStr = (String) params.get("endDate");
        Date startDate = startDateStr != null ? strToData(startDateStr) : null;
        Date endDate = endDateStr != null ? strToData(endDateStr) : null;
        //调用服务层方法分页查找
        Page<EventDetailVo> page = eventDisposeService.pageFaultDisposeByUserId(userId, pageNum, pageSize, status, startDate, endDate);
        //返回查找结果
        return ResponseEntity.ok(page);
    }

    @ApiOperation("工程师分页查找故障处理记录")
    @PostMapping("/pageFaultDispose")
    public ResponseEntity<?> pageFaultDispose(@RequestBody Map<String, Object> params,@RequestHeader("Authorization") String token) {
        //获取页码和页面大小
        Integer pageNum = (Integer) params.get("pageNum");
        Integer pageSize = (Integer) params.get("pageSize");
        //获取状态
        Integer status = (Integer) params.get("status");
        //后续添加一个userId（从token中获取）
        Integer userId = Integer.valueOf(jwtUtil.getValue(token,"userId"));
        //获取正常且正确的startDate、endDate
        String startDateStr = (String) params.get("startDate");
        String endDateStr = (String) params.get("endDate");
        Date startDate = startDateStr != null ? strToData(startDateStr) : null;
        Date endDate = endDateStr != null ? strToData(endDateStr) : null;
        //调用服务层方法分页查找
        Page<EventDetailVo> page = eventDisposeService.pageFaultDispose(userId, pageNum, pageSize, status, startDate, endDate);
        //返回查找结果
        return ResponseEntity.ok(page);
    }

    @ApiOperation("统计工程师待处理故障数")
    @GetMapping("/statisticsCurrentUserFault")
    public ResponseEntity<?> statisticsFault(@RequestHeader("Authorization") String token) {
        //后续添加一个userId（从token中获取）
        Integer userId = Integer.valueOf(jwtUtil.getValue(token,"userId"));

        List<Integer> integers = eventDisposeService.statisticsCurrentUserEvent(userId, EventType.FAULT.getKey());
        return ResponseEntity.ok(integers);
    }
    //故障相关操作====================================================

    private Date strToData(String dateStr) {
        // 直接解析字符串为Instant
        Instant instant = Instant.parse(dateStr);
        // 将Instant转换为java.util.Date
        Date date = Date.from(instant);
        return date;
    }
}
